﻿using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Mvc;
using RotaractProjectCalendar.DataAccessLayer;
using RotaractProjectCalendar.ViewModels;

namespace RotaractProjectCalendar.Areas.Anonymous.Controllers
{
    [AllowAnonymous]
    public class ProjectController : Controller
    {
        private readonly CalendarContext _db = new CalendarContext();
        
        //
        // GET: /Project/
        public ActionResult Index(int? id)
        {
            var viewModel = new ProjectIndexData
            {
                Projects = _db.Projects
                    .OrderBy(p => p.StartDateTime)
            };

            if (id == null)
            {
                return View(viewModel);
            }

            ViewBag.ProjectID = id.Value;
            viewModel.Club = viewModel.Projects.Single(p => p.Id == id.Value).Club;

            return View(viewModel);
        }

        //
        // GET: /Project/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if(id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var project = await _db.Projects.FindAsync(id);
            if(project == null)
            {
                return HttpNotFound();
            }

            return View(project);
        }

        protected override void Dispose(bool disposing)
        {
            if(disposing)
            {
                _db.Dispose();
            }
            base.Dispose(disposing);
        }
	}
}